package dev.kaayf.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import dev.kaayf.utils.Note;

public class NoteDb {
    private DbHelper dbHelper;

    public NoteDb(Context context) {
        dbHelper = new DbHelper(context);
    }

    public long createNote(Note note) {
        SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(DbHelper.COLUMN_NAME, note.getNoteName());
        contentValues.put(DbHelper.COLUMN_COVER, note.getNoteCover());
        contentValues.put(DbHelper.COLUMN_DATE, note.getNoteDate());

        long noteID = sqLiteDatabase.insert(DbHelper.TABLE_NAME, null, contentValues);
        return noteID;
    }

    // filter out all records about notes and return as List<Note>
    public List<Note> getAllNotes() {
        List<Note> noteList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(DbHelper.TABLE_NAME,
                null, null, null, null, null,
                DbHelper.COLUMN_ID + " DESC");

        if (cursor.moveToFirst()) {
            do {
                Note note = new Note();
                note.setNoteId(cursor.getInt(cursor.getColumnIndexOrThrow(DbHelper.COLUMN_ID)));
                note.setNoteName(cursor.getString(cursor.getColumnIndexOrThrow(DbHelper.COLUMN_NAME)));
                note.setNoteCover(cursor.getString(cursor.getColumnIndexOrThrow(DbHelper.COLUMN_COVER)));
                note.setNoteDate(cursor.getString(cursor.getColumnIndexOrThrow(DbHelper.COLUMN_DATE)));
                noteList.add(note);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return noteList;
    }

    public void updateNoteContext(int index, String newContext) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(DbHelper.COLUMN_CONTEXT, newContext);

        String whereClause = DbHelper.COLUMN_ID + " = ?";
        String[] whereArgs = {String.valueOf(index)};

        db.update(DbHelper.TABLE_NAME, values, whereClause, whereArgs);
        db.close();
    }


    public String getNoteContextById(int id) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String context = null;
        Cursor cursor = null;

        String[] columns = {DbHelper.COLUMN_CONTEXT};

        String selection = DbHelper.COLUMN_ID + " = ?";
        String[] selectionArgs = {String.valueOf(id)};


        cursor = db.query(
                DbHelper.TABLE_NAME,
                columns,
                selection,
                selectionArgs,
                null,
                null,
                null
        );

        if (cursor != null && cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndex(DbHelper.COLUMN_CONTEXT);
            if (columnIndex != -1) {
                context = cursor.getString(columnIndex);
            }
            cursor.close();
        }

        db.close();
        return context;
    }

    // delete one record
    public void deleteNote(int index) {
        var db = dbHelper.getWritableDatabase();
        db.execSQL("DELETE FROM " + DbHelper.TABLE_NAME + " WHERE " + DbHelper.COLUMN_ID + " = " + index);
    }
}
